import { VideoHandler } from './handlers/VideoHandler';
import { RecorderHandler } from './handlers/RecorderHandler';
import { BYTEDANCE } from 'cc/env';

export abstract class BasePlatform {
    
    /** 平台句柄 */
    platform: any = null

    /** 系统信息 */
    systemInfo: any = null

    /** appId */
    id: string = ''

    /** appKey */
    key: string = ''

    /** 激励广告 */
    video: string[] = []

    /** 横幅广告 */
    banner: string[] = []

    /** 插屏广告 */
    interstitial: string[] = []

    /** 原生模板 */
    custom: string[] = []

    /** 是否是开发工具 */
    isDevtools: boolean = false

    /** 激励广告控制器 */
    videoHandler: VideoHandler = null

    /** 录屏组件控制器 */
    recorderHandler: RecorderHandler = null

    /**
     * 构造函数
     * @param platform 平台，例如window['tt']，无可为null
     * @param id appId，无可为''
     * @param key appKey，无可为''
     * @param video 激励广告id，无可为[]
     * @param banner 横幅广告id，无可为[]
     * @param interstitial 插屏广告id，无可为[]
     * @param custom 原生模板id,无可为[]
     */
    constructor(platform: any = null, id: string = '', key: string = '', video: string[] = [], banner: string[] = [], interstitial: string[] = [], custom: string[] = []) {
        this.platform = (platform == null || platform == undefined) ? null : platform
        if (this.platform != null) {
            this.systemInfo = this.platform.getSystemInfoSync()
            if (this.systemInfo) {
                this.isDevtools = this.systemInfo.platform === 'devtools'
            }
        }
        this.id = id
        this.key = key
        if (video && video.length > 0) {
            this.video = video

            this.videoHandler = new VideoHandler(this)
            this.videoHandler.init()
        }
        if (banner && banner.length > 0) {
            this.banner = banner
        }
        if (interstitial && interstitial.length > 0) {
            this.interstitial = interstitial
        }
        if (custom && custom.length > 0) {
            this.custom = custom
        }

        if (BYTEDANCE) {
            this.recorderHandler = new RecorderHandler(this)
            this.recorderHandler.init()
        }
    }

    /** 初始化 */
    init() {

    }

    /**
     * 展示时调用
     * @param show 展示时执行的方法
     * @returns 
     */
    onShow(show: any) {
        if (!this.platform) {
            return
        }
        this.platform.onShow(show)
    }

    /**
     * 隐藏时调用
     * @param hide 隐藏时执行的方法
     * @returns 
     */
    onHide(hide: any) {
        if (!this.platform) {
            return
        }
        this.platform.onHide(hide)
    }

    /** 重启游戏 */
    restart() {
        if (!this.platform) {
            return
        }
        this.platform.restartMiniProgram()
    }

    /** 关闭游戏 */
    exit() {
        if (!this.platform) {
            return
        }
        this.platform.exitMiniProgram()
    }

    /**
     * 用户登录
     * @param login 登录时执行的方法
     */
    login(callback?: (success: boolean) => void) {

    }

    /**
     * 展示激励视频
     * @param success 看完后执行的方法
     * @param share 是否是分享
     * @param fail 分享失败后执行的方法
     * @returns 
     */
    showVideo(success: () => void, fail?: () => void, complete?: () => void, share = false) {
        console.log('显示广告1')
        if (this.isDevtools) {
            success()
            return
        }
        console.log('显示广告2')
        if (this.videoHandler) {
            console.log('显示广告3')
            this.videoHandler.show(success, fail, complete, share)
        } else {
            console.log('显示广告4')
            success()
        }
    }

    share(callback?: (success: boolean) => void, channel?: string) {
        this.platform.share(callback, channel)
    }

    /**
     * 震动
     * @param type 震动类型
     * @returns 
     */
    vibrate(type: 'short' | 'long') {
        if (!this.platform) {
            return
        }
        switch (type) {
            case 'short':
                this.platform.vibrateShort()
                break
            case 'long':
                this.platform.vibrateLong()
                break
        }
    }

    /**
     * 汇报埋点
     * @param eventName 事件名称
     * @param value 参数
     */
    reportAnalytics(eventName: string, value: {} = {}) {
        if (!this.platform) {
            return
        }
        this.platform.reportAnalytics(eventName, value)
    }

    startAccelerometer() {
        if (!this.platform) {
            return
        }
        this.platform.startAccelerometer()
    }

    stopAccelerometer() {
        if (!this.platform) {
            return
        }
        this.platform.stopAccelerometer()
    }

    onAccelerometerChange(callback) {
        if (!this.platform) {
            return
        }
        this.platform.onAccelerometerChange(callback)
    }

    offAccelerometerChange(callback?) {
        if (!this.platform) {
            return
        }
        if (callback) {
            this.platform.offAccelerometerChange(callback)
        } else {
            this.platform.offAccelerometerChange()
        }
    }

    addShortcut(callback: (success: boolean) => void) {
        this.platform.addShortcut({
            success() {
              console.log("添加桌面成功");
              callback(true)
            },
            fail(err) {
              console.log("添加桌面失败", err.errMsg);
              callback(false)
            },
        })
    }
}


